package huas.ant.controller;

import huas.ant.common.PageResult;
import huas.ant.common.Result;
import huas.ant.dto.MemberDTO;
import huas.ant.entity.Member;
import huas.ant.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 条件分页列表查询
     */
    @GetMapping("/page")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String name , Short gender){
        log.info("传递的分页参数为：{},{},{},{}",page,pageSize,name,gender);
        PageResult pageResult = memberService.page(page,pageSize,name,gender);
        return Result.success(pageResult);
    }

    /**
     * 添加会员
     */
    @PostMapping
    public Result insert(@RequestBody MemberDTO memberDTO){
        log.info("添加的会员信息为：{}",memberDTO);
        memberService.insert(memberDTO);
        return Result.success();
    }

    /**
     * 根据会员ID查询会员信息
     */
    @GetMapping("/{id}")
    public Result<Member> findById(@PathVariable Integer id){
        log.info("要查询的会员id为：{}",id);
        Member member = memberService.findById(id);
        return Result.success(member);
    }
    /**
     * 修改会员
     */
    @PutMapping
    public Result update(@RequestBody Member member){
        log.info("要修改的会员信息为：{}",member);
        memberService.update(member);
        return Result.success();
    }
    /**
     * 删除会员
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("要删除的会员id为：{}",id);
        memberService.delete(id);
        return Result.success();
    }
}
